package com.webull.information.center.carwler.common.util;

import com.webull.framework.util.UtilDate;
import com.webull.information.center.carwler.common.model.NewsInformation;
import com.webull.securities.client.model.Exchange;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

public class NewsCcUtil {
    public static String genTitleMd5(String title) {
        return Optional.ofNullable(StringUtils.stripToNull(StringUtils.deleteWhitespace(title)))//
                .map(s0 -> s0.hashCode() + "").orElse(null);
    }


    public static void foreSortExchange(String dateStr, List<Exchange> exchanges, Integer... exIds) {
        if (exchanges == null || exchanges.isEmpty() || exIds == null || exIds.length == 0) return;
        if (UtilDate.format(new Date(), "yyyy-MM-dd").compareTo(dateStr) <= 0) {
            List<Exchange> tmps = new ArrayList<>();

            for (int i = exchanges.size() - 1; i >= 0; i--) {
                if (ArrayUtils.contains(exIds, exchanges.get(i).getId())) {
                    tmps.add(exchanges.get(i));
                    exchanges.remove(i);
                }
            }

            for (int i = 0; i < tmps.size(); i++) {
                exchanges.add(0, tmps.get(i));
            }
        }
    }

    public static final String TopNewsPrefix = "ReutersTop_";

    /**
     * 检查物料
     *
     * @return
     */
    public static boolean checkNewsInformation0(NewsInformation info0) {
        if (info0 == null || info0.getNewsTime() == null
                || StringUtils.isEmpty(info0.getUrl())
                || StringUtils.isEmpty(info0.getTitle())
                || StringUtils.isEmpty(info0.getSourceName())
                || StringUtils.isEmpty(info0.getCollectSource()))
            return false;
        return true;
    }




}
